﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace CMS_GYM
{
    class clsPhieuChiTienChiTietDAO
    {
        #region Attribute
        /// <summary>
        /// Kết nối SQL Server
        /// </summary>
        private CSQLServer sqlServer = new CSQLServer();
        #endregion


        public DataTable LayBang(string SoPhieuChi, string KyHieu)
        {
            string sql = "spGetInfoPhieuChiChiTietTheoSoPhieuChiVaKyHieu";
            string[] ParameterColection = new string[] {    
                                    "@SoPhieuChi","@KyHieu"
                                };
            Object[] valueofParameter = new Object[] {
                                    SoPhieuChi,KyHieu
                                };
            DataTable table = sqlServer.readData(sql, ParameterColection, valueofParameter);
            return table;
        }

        public DataTable LayBang(decimal PhieuChiId)
        {
            string sql = "spGetBangPhieuChiChiTiet";
            string[] ParameterColection = new string[] { "@PhieuChiId" };
            Object[] valueofParameter = new Object[] { PhieuChiId };
            DataTable table = sqlServer.readData(sql, ParameterColection, valueofParameter);
            return table;
        }

        /// <summary>
        /// Lay danh sach phieu thu chi tiet theo phieu thu id
        /// </summary>
        /// <param name="PhieuChiId"></param>
        /// <returns></returns>
        public List<clsPhieuChiTienChiTietDTO> LayDanhSach(decimal PhieuChiId)
        {
            List<clsPhieuChiTienChiTietDTO> CacPTCT = new List<clsPhieuChiTienChiTietDTO>();
            DataTable table = LayBang(PhieuChiId);
            if (table == null)
            {
                return null;
            }
            else
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    clsPhieuChiTienChiTietDTO ptct = ChuyenDoi(table.Rows[i]);
                    if (ptct == null)
                    {
                        return null;
                    }
                    else
                    {
                        CacPTCT.Add(ptct);
                    }
                }
                return CacPTCT;
            }
        }

        /// <summary>
        /// Lay danh sach phieu thu chi tiet theo so phieu thu va ky hieu
        /// </summary>
        /// <param name="SoPhieuChi"></param>
        /// <param name="KyHieu"></param>
        /// <returns></returns>
        public List<clsPhieuChiTienChiTietDTO> LayDanhSach(string SoPhieuChi, string KyHieu)
        {
            List<clsPhieuChiTienChiTietDTO> CacPTCT = new List<clsPhieuChiTienChiTietDTO>();
            DataTable table = LayBang(SoPhieuChi, KyHieu);
            if (table == null)
            {
                return null;
            }
            else
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    clsPhieuChiTienChiTietDTO ptct = ChuyenDoi(table.Rows[i]);
                    if (ptct == null)
                    {
                        return null;
                    }
                    else
                    {
                        CacPTCT.Add(ptct);
                    }
                }
                return CacPTCT;
            }
        }

        private clsPhieuChiTienChiTietDTO ChuyenDoi(DataRow row)
        {
            clsPhieuChiTienChiTietDTO PhieuChi = new clsPhieuChiTienChiTietDTO();
            try
            {
                PhieuChi.PhieuChiId = decimal.Parse(row["PhieuChiId"].ToString());
                PhieuChi.PhieuChiChiTietId = decimal.Parse(row["PhieuChiChiTietId"].ToString());
                PhieuChi.NoiDung = row["NoiDung"] == null ? string.Empty : row["NoiDung"].ToString();
                PhieuChi.TienChi = double.Parse(row["TienChi"].ToString());
                PhieuChi.TienHang = double.Parse(row["TienHang"].ToString());
                PhieuChi.TienThue = double.Parse(row["TienThue"].ToString());
                PhieuChi.TaiKhoanCo = row["TaiKhoanCo"] == null ? string.Empty : row["TaiKhoanCo"].ToString();
                PhieuChi.TaiKhoanNo = row["TaiKhoanNo"] == null ? string.Empty : row["TaiKhoanNo"].ToString();

                return PhieuChi;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Thêm thông tin chi tiết phiếu chi 
        /// </summary>
        /// <param name="ChiTietPhieuXuất">
        /// MaPhieuXuat  nvarchar(10)
        /// MaMatHang  nvarchar(10)
        /// SoLuong  int
        /// DonGia  float
        /// ChietKhau  float
        /// ThanhTien  float
        /// ThueVAT  float
        /// </param>
        public int Them(clsPhieuChiTienChiTietDTO PCCT)
        {
            int i = -1;
            string sql = "spInsertPhieuChiChiTiet";
            string[] ParameterColection = new string[] {    
                                    "@PhieuChiId","@NoiDung","@TienChi","@TienHang",
                                    "@TienThue","@TaiKhoanNo","@TaiKhoanCo"
                                };
            Object[] valueofParameter = new Object[] {
                                   PCCT.PhieuChiId, PCCT.NoiDung, PCCT.TienChi,PCCT.TienHang,
                                   PCCT.TienThue, PCCT.TaiKhoanNo, PCCT.TaiKhoanCo
                                };

            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Sua mot phieu chi chi tiêt
        /// </summary>
        /// <param name="PTCT"></param>
        /// <returns></returns>
        public int Sua(clsPhieuChiTienChiTietDTO PTCT)
        {
            int i = -1;
            string sql = "spUpdatePhieuChiChiTiet";
            string[] ParameterColection = new string[] {    
                                    "@PhieuChiChiTietId","@NoiDung","@TienChi","@TienHang",
                                    "@TienThue","@TaiKhoanNo","@TaiKhoanCo"
                                };
            Object[] valueofParameter = new Object[] {
                                   PTCT.PhieuChiChiTietId, PTCT.NoiDung, PTCT.TienChi,PTCT.TienHang,
                                   PTCT.TienThue,  PTCT.TaiKhoanNo, PTCT.TaiKhoanCo
                                };

            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Xoa mot phieu chi chi tiêt
        /// </summary>
        /// <param name="PTCT"></param>
        /// <returns></returns>
        public int Xoa(decimal PhieuChiId)
        {
            int i = -1;
            string sql = "spDeletePhieuChiChiTiet";
            string[] ParameterColection = new string[] { "@PhieuChiId" };
            Object[] valueofParameter = new Object[] { PhieuChiId };

            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }
    }
}
